import java.io.*;
import java.net.*;


public class HttpThreadPool {
	
	private int numThreads;
	
	public HttpThreadPool(final ServerSocket server) {
		numThreads = Integer.parseInt(System.getProperty(HTTPconf.serverName,HTTPconf.maxConnection));
		
		for(int i=0; i<(numThreads-1); i++) {
			Thread thread = new Thread() {
				public void run() {
					dispatchLoop(server);
				}
			};
			thread.start();
		}
	}
	
	public void dispatchLoop(ServerSocket server) {
		while(true) {
			try {
				HttpProcessor processor = null;
				
				Socket con = server.accept();
				HttpInputStream his = 
					new HttpInputStream(con.getInputStream());
				his.getRequest();
				HttpOutputStream hos = 
					new HttpOutputStream(con.getOutputStream(), his.getVersion(), his.getMethod());
				if(his.getResourcePath().startsWith(HTTPconf.scriptDir.getPath())) {
//					processor = new ScriptProcessor(his);
					processor.process(hos);
				} else {
					processor = new FileProcessor(his);
					processor.process(hos);
				}
			} catch(IOException ioe) {
				ioe.printStackTrace();
			}
		}
	}
}
